import {
  Controller,
  Delete,
  Query,
  Post,
  Get,
  Body,
  Param,
} from '@nestjs/common';
import { TopicService } from './topic.service';
import { CreateTopicDto } from './dto/create-topic.dto';
import { Topic } from './entities/topic.entity';
import { ApiOperation, ApiResponse, ApiTags, ApiParam } from '@nestjs/swagger';
import { Response } from '../../infra/schema/response';
import { Public } from 'src/auth/auth.decorator';

@ApiTags('topics')
@Controller('topics')
export class TopicController {
  constructor(private readonly topicService: TopicService) {}

  @ApiOperation({ summary: '创建话题' })
  @ApiResponse({ status: 201, description: '话题创建成功', type: Topic })
  @Public()
  @Post()
  create(@Body() createTopicDto: CreateTopicDto): Promise<Topic> {
    console.log(createTopicDto, 'createTopicDtoooooooooooooooo');
    return this.topicService.create(createTopicDto);
  }

  @Public()
  @Get()
  @ApiOperation({ summary: '获取所有话题' })
  @ApiResponse({
    status: 200,
    description: '成功获取所有话题',
    type: [Topic],
  })
  async findAll(
    @Query('page') page: number = 1,
    @Query('size') size: number = 10,
    @Query('title') title: string = '',
    @Query('userId') userId?: number, // 标记为可选参数
  ) {
    const query = { page, size, title, userId };
    const result = await this.topicService.getList(query);
    return Response.successList(result.data, result.total);
  }

  @Public()
  @Get(':topicId')
  @ApiOperation({ summary: '获取话题信息及用户收藏状态' })
  @ApiResponse({ status: 200, description: '成功获取话题信息及收藏状态' })
  async getTopic(
    @Param('topicId') topicId: number,
    @Query('userId') userId: number, // 新增查询参数 userId
  ) {
    return this.topicService.getTopicWithFavoriteStatus(topicId, userId);
  }

  @Delete(':id')
  @ApiOperation({ summary: '根据 ID 删除话题' })
  @ApiParam({ name: 'id', description: '话题 ID', type: Number })
  @ApiResponse({ status: 200, description: '话题删除成功' })
  @ApiResponse({ status: 404, description: '未找到对应话题' })
  async remove(@Param('id') id: string) {
    const result = await this.topicService.remove(+id);
    return Response.success(result);
  }
}
